package gases.as3.librarys.components.ui.container
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.librarys.components.ui.module.PanelPoolManage;
	
	public class GPanelWithTitle extends PanelPoolManage
	{
		public function GPanelWithTitle(pw:Number,ph:Number,tw:Number,th:Number,tname:String = "",tcolor:uint = 0x000000)
		{
			_pWidth = pw;
			_pHeight = ph;
			_tWidth = tw;
			_tHeight = th;
			_tName = tname;
			_tColor = tcolor;
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			
			_panel = new GPanel(_pWidth,_pHeight);
			_panel.setMoveState(false);
			pushDisplayObject(_panel);
			
			
			
			_title = new GTitle(_tWidth,_tHeight,_tName,_tColor);
			pushDisplayObject(_title);
			_title.mouseChildren = true;
			_title.mouseEnabled = true;
			moveState();
		}
		
		public function setSkin(panelSkin:BaseSkin,titleSkin:BaseSkin):void
		{
			_panel.skin = panelSkin;
			_title.skin = titleSkin;
		}
		
		public function setMoveState(bool:Boolean):void
		{
			_canMove = bool;
			moveState();
		}
		
		private function moveState():void
		{
			if(_canMove)
			{
				addMouseListeners();
			}
			else
			{
				removeMouseListenrs();
			}
		}
		
		private function addMouseListeners():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			this.addEventListener(MouseEvent.CLICK,onClickHandler);
		}
		
		private function removeMouseListenrs():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			this.removeEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onClickHandler);
		}
		
		/**
		 * 移动ui 
		 * @param e
		 * 
		 */		
		private function onMouseDownHandler(e:MouseEvent):void
		{
			this.startDrag();
		}
		
		/**
		 * 停止移动ui 
		 * @param e
		 * 
		 */		
		private function onMouseUpHandler(e:MouseEvent):void
		{
			this.stopDrag();
		}
		
		/**
		 * 点击panel并发送事件，用来展示在最前面 
		 * @param e
		 * 
		 */		
		private function onClickHandler(e:MouseEvent):void
		{
			this.dispatchEvent(new Event("panelWithTitleClick"));
		}
		
		override public function dispose():void
		{
			removeMouseListenrs();
			this.removeChild(_panel);
			_panel.dispose();
			_panel = null;
			this.removeChild(_title);
			_title.dispose();
			_title = null;
			super.dispose();
		}
		
		public function get vAlign():String
		{
			return _vAlign;
		}
		
		public function set vAlign(value:String):void
		{
			_vAlign = value;
		}
		
		public function get hAlign():String
		{
			return _hAlign;
		}
		
		public function set hAlign(value:String):void
		{
			_hAlign = value;
		}
		
		private var _vAlign:String;
		private var _hAlign:String;
		
		private var _pWidth:Number;
		private var _pHeight:Number;
		private var _tWidth:Number;
		private var _tHeight:Number;
		private var _tName:String;
		private var _tColor:uint;
		
		private var _panel:GPanel;
		private var _title:GTitle;
		
		private var _canMove:Boolean = true;
	}
}